#!/usr/bin/env python3
from __future__ import print_function
import xlrd
from datetime import datetime, date
import re
import sys
import os
from excel import Gen_XLS
from gen import Get_ID
#from xls2pdf import xls2pdf
from mail import send_mail
import html
import re
import html2pdf

if len(sys.argv) != 3:
    print('	{} [input.excel] [start-row-number]'.format(sys.argv[0]))
    sys.exit(-1)

start = int(sys.argv[2]) - 1
total = 1

rb = xlrd.open_workbook(sys.argv[1])
sheet = rb.sheet_by_index(0)
rows = sheet.nrows
cols =  sheet.ncols
#0 empty 1 string 2 number 3 date
#for x in range(start, rows):
for x in range(start, start + total):
    test_date = ""
    test_date_month = 0
    test_date_day = 0
    test_date_year = 2020
    first_name, last_name = "", ""
    birth = ""
    tel = ""
    email = ""
    price = 0
    start = ord('A')

    for y in range(cols):
        cell = sheet.cell(x, y)
      
        if y == (ord('C')  - start) and cell.ctype == 2:
            tmp = cell.value
            price = "{:.0f}".format(tmp) 
            print(price, end="|")

        if y == (ord('F')  - start) and cell.ctype == 3:
            cell_tuple = xlrd.xldate_as_tuple(cell.value, datemode=0)
            test_date = (date(*cell_tuple[:3]).strftime('%Y/%m/%d'))

        if y == (ord('L')  - start) and cell.ctype == 1:
            how = cell.value.count(' ')
            second_name = ""
            if how == 1:
                first_name, last_name = cell.value.split(' ')
            elif how == 2:
                first_name, second_name, last_name = cell.value.split(' ')
            first_name = first_name + " " + second_name
            first_name = first_name.strip()
            last_name = last_name.strip()
            print(first_name , last_name, end="|")

        if y == (ord('M')  - start) and cell.ctype == 3:
            cell_tuple = xlrd.xldate_as_tuple(cell.value, datemode=0)
            birth = (date(*cell_tuple[:3]).strftime('%m/%d/%Y'))
            print(birth, end="|")


        if y == (ord('O')  - start) and cell.ctype == 1:
            tel = cell.value
            print(tel, end="|")
        if y == (ord('O')  - start) and cell.ctype == 2:
            tmp = cell.value
            tel = "{:.0f}".format(tmp) 
            print(tel, end="|")

        if y == (ord('Q')  - start) and cell.ctype == 1:
            email = cell.value
            print(email, end="|")
       

    data_dict = {
   'last_name': last_name,  #first name
   'first_name': first_name,  #second name
   'dob': birth,
   #'gender': 'M',
   'email': email,  #email
   'phone': tel, #phone
   'sample_date': test_date,
   'price':price,
   'GScode':Get_ID(test_date)
    }
    ok = re.findall(r"(20\d{2}/\d{2}/\d{2})", test_date)
    if len(ok) <= 0:
        sys.exit(0)
    print(data_dict)
    html.Gen_HTML(**data_dict)
    fullname = first_name + "_" + last_name
    filename = fullname + "-" + email + ".htm"
    dirname = "out/"+ test_date
    outtopdf = os.path.join(dirname, filename)
    html2pdf.HTML2PDF(outtopdf)
    tmptopdf = os.path.join("tmp", filename)
    html2pdf.HTML2PDF(tmptopdf)


